package com.click.app.dao.hibernate;

import com.click.app.dao.PurchaseDao;
import com.click.app.model.Purchase;
import com.click.app.model.Supplier;
import com.click.app.service.SupplierManager;
import java.util.Date;
import java.util.List;


/**
 * @author Joel Chuca Condo
 * @date 04/05/2009
 */
public class PurchaseDaoHibernate extends GenericDaoHibernate<Purchase, Long> implements PurchaseDao {

    private SupplierManager supplierManager;

    public PurchaseDaoHibernate() {
        super(Purchase.class);
    }
    
    public List<Purchase> findPurchasesUnPaid() {
        List<Purchase> purchases = getHibernateTemplate().find("Select purchase from Purchase as purchase " +
                "where purchase.restPrice<>0");
        return purchases;
    }



    // ------------ reports method -------------

    public Purchase findTotalMountPurchase(Supplier supplier) {
        List<Purchase> parcialPurchases = getHibernateTemplate().find("Select purchase from Purchase as purchase " +
                "where purchase.supplier.id ="+supplier.getId());
        Purchase totalPurchase = new Purchase();
        totalPurchase.setTotalPrice(new Double(0));
        totalPurchase.setRestPrice(new Double(0));
        totalPurchase.setDiscount(new Double(0));
    
        for (Purchase purchase : parcialPurchases) {
            if(purchase.getTotalPrice() != null)
                totalPurchase.setTotalPrice(totalPurchase.getTotalPrice()+purchase.getTotalPrice());
            if(purchase.getRestPrice() != null)
                totalPurchase.setRestPrice(totalPurchase.getRestPrice()+purchase.getRestPrice());
            if(purchase.getDiscount() != null)
                totalPurchase.setDiscount(totalPurchase.getDiscount()+purchase.getDiscount());
        }
        return totalPurchase;
    }

    public void setSupplierManager(SupplierManager supplierManager) {
        this.supplierManager = supplierManager;
    }

    public Double getTotalMonthPriceBySupplier(int month, int year, Supplier supplier) {
        Double total = 0D;
        List<Purchase> purchases = (List<Purchase>) getHibernateTemplate().find("from Purchase where supplier=?", supplier);
        for (Purchase p : purchases) {

            if ((p.getDate().getMonth() == month) && (p.getDate().getYear() == year)) {
                total = total + p.getTotalPrice();
            }
        }
        return total;
    }

    public Object suppliersReport(Date begin, Date end) {
        
        List<Supplier> suppliers = (List<Supplier>) getHibernateTemplate().find("from Supplier");
        int x=0;
        if (end.getYear() == begin.getYear()) {
            x = end.getMonth() - begin.getMonth();
        } else {
            x = (12 + ((int) (end.getMonth()))) - (begin.getMonth());
        }
        int y = suppliers.size();
        Double[][] sr = new Double[x][y];

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                int m=((Integer) begin.getMonth())+i;
                int ye=((Integer) begin.getYear());
                if (m>11){
                    m=m%12;
                    ye++;
                }
                sr[i][j] = getTotalMonthPriceBySupplier(m, ye, suppliers.get(j));
            }
        }

        for (int i = 0; i < x; i++) {
            System.out.print("Month: ");
            for (int j = 0; j < y; j++) {
                System.out.print(sr[i][j] + ", ");
            }
            System.out.println("");

        }

        return sr;
    }

}
